//
//  APPLEStatusCell.swift
//  123
//
//  Created by pangjiaxing on 16/1/2.
//  Copyright © 2016年 pangjiaxing. All rights reserved.
//

import UIKit
import SnapKit
let AppleStatusCellMargin:CGFloat = 8
let AppleStatusIconViewH: CGFloat = 35

class APPLEStatusCell: UITableViewCell {
    // toolBar 的顶部约束
    var toolBarTopConstraint: Constraint?
    
    private lazy var statusOriginalView : APPLEStatusOriginalView = APPLEStatusOriginalView()
   
    private lazy var statusToolBar: APPLEStatusToolBarView = APPLEStatusToolBarView()
    
    private lazy var statusRetweetView: APPLEStatusRetweetView = APPLEStatusRetweetView()

    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var statusViewModel:APPLEStatusViewModel?{
        didSet{
            statusOriginalView.statusViewModel=statusViewModel
            statusToolBar.statusViewModel=statusViewModel
            self.toolBarTopConstraint?.uninstall()
            if (statusViewModel?.status?.retweeted_status != nil) {
                statusRetweetView.hidden = false
                statusRetweetView.statusViewModel=statusViewModel
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
                })
            }else{
                statusRetweetView.hidden = true
                statusToolBar.snp_updateConstraints { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                }
            }
            statusRetweetView.statusViewModel = statusViewModel
        }
    }
    private func setupUI(){
        
        // 添加视图
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusRetweetView)
        contentView.addSubview(statusToolBar)
        
        // 添加约束
        statusOriginalView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.top.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        statusRetweetView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(statusOriginalView)
            make.trailing.equalTo(statusOriginalView)
            make.top.equalTo(statusOriginalView.snp_bottom)
        }
        
        statusToolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
            self.toolBarTopConstraint = make.top.equalTo(statusRetweetView.snp_bottom).constraint
            make.height.equalTo(35)
        }
        
        // 非常重要
        contentView.snp_makeConstraints { (make) -> Void in
            // 决定 cell 的高度
            make.bottom.equalTo(statusToolBar.snp_bottom)
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.trailing.equalTo(self)
        }
    }
}
